/*	Brian Kirkpatrick
	QuX result class
	Begun 6/30/2010
*/

#include "quxResult.h"

quxResult::quxResult() {
	// Create empty results list
	size = 0;
	results = NULL;
}

quxResult::quxResult(quxResult &qr) {
	// Deep copy result qr
	size = qr.size;
	results = new stringDict[size];
	for (int i = 0; i < size; i++) {
		results[i] = qr[i];
	}
}

quxResult::~quxResult() {
	// Free results list if set
	if (results != NULL) {
		delete[] results;
		results = NULL;
	}
}

stringDict quxResult::operator[](int ndx) {
	// Returns results at given index, if within bounds
	if (ndx >= 0 && ndx < size) return results[ndx];
	else return stringDict();
}

quxResult quxResult::operator+(stringDict operand) {
	// Adds the operand to the string dictionaries in this result
	quxResult toReturn = quxResult();
	toReturn.size = size+1;
	toReturn.results = new stringDict[toReturn.size];
	for (int i = 0; i < size; i++) {
		toReturn.results[i] = results[i];
	}
	toReturn.results[toReturn.size-1] = operand;
	return toReturn;
}

quxResult quxResult::operator+(quxResult operand) {
	quxResult toReturn = quxResult();
	toReturn.size = getSize() + operand.getSize();
	toReturn.results = new stringDict[toReturn.size];
	for (int i = 0; i < getSize(); i++) {
		toReturn.results[i] = results[i];
	}
	for (int j = 0; j < operand.getSize(); j++) {
		toReturn.results[getSize()+j] = operand.results[j];
	}
	return toReturn;
}

quxResult & quxResult::operator=(const quxResult &operand) {
	if (results != NULL) {
		delete[] results;
		results = NULL;
	}

	size = operand.size;
	results = new stringDict[size];

	for (int i = 0; i < size; i++) {
		results[i] = operand.results[i];
	}
	return *this;
}

void quxResult::print() {
	// Prints each result
	for (int i = 0; i < size; i++) {
		printf("[%d] => ", i);
		results[i].print();
		printf("\n");
	}
}

int quxResult::getSize() {
	// Returns number of result entries
	return size;
}
